import { IBaseContentPage } from "../Lib/IBaseIssuePage";
import { SSLib } from "../Lib/SSLib";
import { IssuesPage } from "./issuesPage";
import { BranchesPage } from "./branchesPage";
import { MergeRequestsNew } from './mergeRequestsNew';
import { MergeRequestsPage } from './mergeRequests';
import { IssuesNew } from './issuesNew';
import { RepositoryEeditorPage } from './Rap2/RepositoryEditor';

let allContentPages: IBaseContentPage[] = [
    new IssuesPage(),
    new BranchesPage(),
    new MergeRequestsNew(),
    new MergeRequestsPage(),
    new IssuesNew(),
    new RepositoryEeditorPage()
];

let curUrl = location.href.toLowerCase();

SSLib.getGitlabConfig(config => {
    if (curUrl.indexOf(config.gitlabUrl) !== 0 && curUrl.indexOf(config.rap2Url) !== 0) {
        return;
    }

    allContentPages.forEach(item => {
        if (item.IsMatch(curUrl)) {
            setTimeout(() => {
                item.Execute();
            }, 1000);
            return false;
        }
    });
});

let methodHandler: any = {};
let msgType = "content";
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
    let cmdType = request.cmdType;
    let argus = request.argus || [];
    argus.push(sendResponse);

    if (cmdType.indexOf(msgType + ".") !== 0) {
        return;
    }

    let innerCmdType = cmdType.substr(msgType.length + 1);

    methodHandler[innerCmdType].apply(methodHandler, argus);
});

console.log(location.href + '121113');
